<?php

class Core_Backend_Component_Foundation_Controller_Helper extends Core_Backend_Component_Foundation_Controller_Poi {

    public function poiBrowserAction($title = '', $ignores = array(), $options = array(), $filter = array()){
        return $this->viewResult(array(
            'title'=>$title,
            'ignores'=>$ignores,
            'options'=>$options, 
            'filter'=>$filter));
    }

    public function poiListAction($pageindex = 1) {
        $validator = $this->getRequest()->getValidator('filter');
        $ignores = $this->getRequest()->getParameter('ignores');
        $collection = App::getModel('foundation/collection_poi')
                        ->pagination($pageindex, 50);
		if(!$this->getIdentity()->isInRole('resource_manager')){
			$collection->innerJoin('{t}account_poi', 'g', 'g.poi_id', 'a.id')
					   ->eq('g.account_id', $this->getIdentity()->getId());
		}                        
        if($validator->isNotEmpty('picture')){
            if($validator->getValue('picture') == '1'){
                $collection->filterByHasPicture();
            }else{
                $collection->filterByNoPicture();
            }
        }
        if ($validator->isNotEmpty('road')) {
            $collection->filterByRoadId($validator->getValue('road'));
        }
        if ($validator->isNotEmpty('industry')) {
            $collection->filterByIndustryId($validator->getValue('industry'));
        }
        if (!empty($ignores)) {
            Kd_Util_Array::removeEmpty($ignores);
            $collection->not()->contain('a.id', ':ids:s')->addParameter('ids', $ignores);
        }
        $collection->load();
        $roadStat = NULL;
        if($validator->isNotEmpty('industry') || $validator->isNotEmpty('road')){
            $sql = "select a.industry_id, a.road_id, count(id) as poi_count from {t}foundation_poi a";
			if(!$this->getIdentity()->isInRole('resource_manager')){
				$sql .= ' inner join {t}account_poi g on g.poi_id = a.id';
			}                 
			$sql .= " where 1=1 ";
			if(!$this->getIdentity()->isInRole('resource_manager')){
				$sql .= ' and g.account_id='.$this->getIdentity()->getId();
			}
            if($validator->isNotEmpty('industry')){
                $sql .= " and a.industry_id like '%".$validator->getValue('industry')."%'";
            }
            if ($validator->isNotEmpty('road')) {
                $sql .= " and a.road_id like '%" . $validator->getValue('road') . "%'";
            }
            if($validator->isNotEmpty('picture')){
                if($validator->getValue('picture') == '1'){
                    $sql .= ' and not isnull(a.photo)';
                }else{
                    $sql .= ' and isnull(a.photo)';
                }
            }
            $sql .= " group by road_id order by poi_count desc";
            $roadStat = Kd_Db_Factory::getDriver()->setSql($sql)->fetch()->toArray();
        }
        return $this->viewResult(array('list' => $collection, 'records'=>$collection->loadCount(), 'road_stat'=>$roadStat));
    }

//    public function choosePoiAction($elementid = '', $callback = '', $road = '', $industry = '', $selected = '', $title = '', $isself = FALSE) {
//        $validator = new Kd_Validation_Validator();
//        $validator->setTarget($this->getRequest()->getParameter('filter'));
//        $collection = App::getModel('foundation/collection_poi')->pagination(1, 50);
//        if ($validator->isNotEmpty('shopname')) {
//            $collection->filterByShopName($validator->getValue('shopname'));
//        }
//        if ($validator->isNotEmpty('poiid')) {
//            $collection->filterByPoiId($validator->getValue('poiid'));
//        }
//        if ($validator->isNotEmpty('industryname')) {
//            $collection->filterByIndustryName($validator->getValue('industryname'));
//        }
//        if ($validator->isNotEmpty('townstreetname')) {
//            $collection->filterByTownstreetName($validator->getValue('townstreetname'));
//        }
//        if ($validator->isNotEmpty('roadid')) {
//            $collection->filterByRoadId($validator->getValue('roadid'));
//        }
//        if ($validator->isNotEmpty('industryid')) {
//            $collection->filterByIndustryId($validator->getValue('industryid'));
//        }
//        if ($validator->isNotEmpty('roadname')) {
//            $collection->filterByRoadName($validator->getValue('roadname'));
//        }
//        if ($validator->isNotEmpty('districtname')) {
//            $collection->filterByDistrictName($validator->getValue('districtname'));
//        }
//        if (!empty($industry)) {
//            $collection->contain('industry_id', ':industryIds:s')
//                    ->addParameter('industryIds', explode(',', $industry));
//        }
//        if (!empty($road)) {
//            $collection->contain('road_id', ':roadIds:s')
//                    ->addParameter('roadIds', explode(',', $road));
//        }
//
//        if (!empty($selected)) {
//            $ids = explode(',', $selected);
//            Kd_Util_Array::removeEmpty($ids);
//            if (!empty($ids)) {
//                $collection->not()->contain('a.id', ':ids:s')->addParameter('ids', $ids);
//            }
//        }
//        $collection->load();
//        return $this->viewResult(
//                array('list' => $collection,
//                    'element_id' => $elementid,
//                    'callback' => $callback,
//                    'selected' => $selected,
//                    'is_self' => $isself,
//                    'road' => $road,
//                    'industry' => $industry,
//                    'title' => $title));
//    }
//
//    public function chooseRoadAction($sort = 'id', $dir = 'asc', $elementid = '', $callback = '', $selected = '', $title = '', $isself = FALSE) {
//        $validator = new Kd_Validation_Validator();
//        $validator->setTarget($this->getRequest()->getForm('filter'));
//        $collection = App::getModel('foundation/collection_road')
//                        ->sortBy($sort, $dir)
//                        ->pagination(1, 50);
//        if ($validator->isNotEmpty('provincename')) {
//            $collection->filterByProvinceName($validator->getValue('provincename'));
//        }
//        if ($validator->isNotEmpty('cityname')) {
//            $collection->filterByCityName($validator->getValue('cityname'));
//        }
//        if ($validator->isNotEmpty('distinctname')) {
//            $collection->filterByDistinctName($validator->getValue('distinctname'));
//        }
//        if ($validator->isNotEmpty('townstreetname')) {
//            $collection->filterByTownstreetName($validator->getValue('townstreetname'));
//        }
//        if ($validator->isNotEmpty('roadname')) {
//            $collection->filterByRoadName($validator->getValue('roadname'));
//        }
//
//        if (!empty($selected)) {
//            $ids = explode(',', $selected);
//            Kd_Util_Array::removeEmpty($ids);
//            if (!empty($ids)) {
//                $collection->not()->contain('a.id', ':ids:s')
//                        ->addParameter('ids', $ids);
//            }
//        }
//        $collection->load();
//        return $this->viewResult(
//                array('list' => $collection,
//                    'element_id' => $elementid,
//                    'callback' => $callback,
//                    'selected' => $selected,
//                    'is_self' => $isself,
//                    'title' => $title));
//    }
//
//    public function chooseRituIndustry($code = '') {
//        $codeLength = strlen($code);
//        $list = App::getModel('foundation/collection_rity_industry')
//                        ->fields('name1', 'name2', 'name3', 'name4')
//                        ->field(Kd_Data_Dcl_Fn::stringSub('id', 1, 2), 'id1')
//                        ->field(Kd_Data_Dcl_Fn::stringSub('id', 3, 2), 'id2')
//                        ->field(Kd_Data_Dcl_Fn::stringSub('id', 5, 2), 'id3')
//                        ->field(Kd_Data_Dcl_Fn::stringSub('id', 7, 2), 'id4')
//                        ->like('id', ':code:s')
//                        ->addParameter('code', "%$code%")
//                        ->load();
//        return $this->jsonResult($list->toArray());
//    }

}